package royalpay

import (
    "fmt"
	"time"
	"errors"
	"thirdpaysdk/core"
	"thirdpaysdk/utils/signature"
	//"github.com/astaxie/beego"
)

type QueryRefundResponse struct {
	ReturnCode      string  `json:"return_code"`
	ReturnMsg       string  `json:"return_msg,omitempty"`
	ResultCode      string  `json:"result_code"`
	RefundId        string  `json:"refund_id"`
	PartnerRefundId string  `json:"partner_refund_id"`
	Amount          int     `json:"amount"`
	Currency        string  `json:"currency"`
}

func QueryRefund(partnerCode,orderId,credentialCode,refundId string)(rsp *QueryRefundResponse, err error){
	incompleteURL := "https://mpay.royalpay.com.au/api/v1.0/gateway/partners/%s/orders/%s/refunds/%s?time=%d&nonce_str=%s&sign=%s"
	
	rsp = &QueryRefundResponse{}
	curTime := time.Now().Unix()*1000
	nonceStr := signature.GetNonceStr(20)
	sign := signature.MakeSha256Sign(partnerCode,curTime,nonceStr,credentialCode)
	serverUrl := fmt.Sprintf(incompleteURL, partnerCode, orderId, refundId, curTime, nonceStr, sign)
	client := core.GetPayJSONClient("GET", serverUrl)
	tbdata, e := client.PayJsonExcute(nil, rsp)
    if e != nil {
		err = e
		return
	}
	if rsp.ReturnCode != "SUCCESS" {
		err = errors.New(string(tbdata))
        return
	}
	return
}
